package com.atguigu.chapter11;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/7/24 10:03
 */
public class Flink07_Sql_Kafka {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
    
        
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        
        // 1. 建一张动态表与kafka的topic进行关联(source)
        tenv.executeSql("create table sensor(" +
                            "   id string, " +
                            "   ts  bigint," +
                            "   vc int" +
                            ")with(" +
                            "   'connector' = 'kafka', " +
                            "   'topic'  =   's1',  " +
                            "   'properties.bootstrap.servers'  =   'hadoop162:9092',  " +
                            "   'properties.group.id'  =   'Flink07_Sql_Kafka',  " +
                            "   'scan.startup.mode' = 'latest-offset', " +
                            "   'format' = 'csv'" +
                            ")");
    
        Table table = tenv.sqlQuery("select * from sensor");
    
    
    
        // 1. 建一张动态表与kafka的topic进行关联(sink)
        tenv.executeSql("create table s2(" +
                            "   id string, " +
                            "   ts  bigint," +
                            "   vc int" +
                            ")with(" +
                            "   'connector' = 'kafka', " +
                            "   'topic'  =   's2',  " +
                            "   'properties.bootstrap.servers'  =   'hadoop162:9092',  " +
                            "   'sink.partitioner' = 'fixed', " +
                            "   'value.format' = 'json'" +
                            ")");
        //table.executeInsert("s2");
        tenv.executeSql("insert into s2 select * from " + table);
        
       
    }
}
